﻿using System;
using System.Collections.Generic;
using System.Threading;
using Kobets.Automation.Chimera.Common.Contracts;

namespace Kobets.Automation.Chimera.Common.Commands
{
    public class CommandBase : ICommand
    {
        public event EventHandler<EventArgs<Guid>> CommandComplete;
        public event EventHandler<EventArgs<KeyValuePair<Guid, Exception>>> CommandFailed;
        public event EventHandler<EventArgs<Guid>> CommandInProgress;

        public virtual void Execute(string content, Guid stepId, CancellationToken token) {}
        public virtual void Abort() {}
        public virtual string SerializeContent(dynamic content) { return content; }
        public virtual dynamic DeserializeContent(string content) { return content; }

        protected virtual void OnCommandComplete(EventArgs<Guid> e)
        {
            if (CommandComplete != null)
            {
                CommandComplete(this, e);
            }
        }

        protected virtual void OnCommandFailed(EventArgs<KeyValuePair<Guid, Exception>> e)
        {
            if (CommandFailed != null)
            {
                CommandFailed(this, e);
            }
        }

        protected virtual void OnCommandInProgress(EventArgs<Guid> e)
        {
            if (CommandInProgress != null)
            {
                CommandInProgress(this, e);
            }
        }
    }
}
